<?php
    class Admincp_WebinfoController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_locale;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->view->headTitle('Quản lý website');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Webinfo();
            $this->_locale = 'vi';
        }
        
        public function preDispatch()
		{
		    $auth = Zend_Auth::getInstance();
		    if(!$auth->hasIdentity()) {
		        if($this->_request->getActionName() != 'login')
		            $this->_redirect('admincp/index/login');
		    }
		}
        
        public function indexAction()
        {
            $id = 1;
            $language = new Admincp_Model_Language();
            $this->view->email = $this->_model->getEmailById($id);
            $this->view->data = $this->_model->getWebinfoById($id);
            $this->view->language = $language->listLanguageFull();
            $this->view->id = $id;
            
            if($this->_request->isPost()) {
                $strTrim = new Zend_Filter_StringTrim();
                $email = $strTrim->filter(strtolower(strip_tags($this->_arrParam['email'])));
                $footer['vi'] = nl2br(strip_tags($strTrim->filter($this->_arrParam['footer-vi'])));
                $footer['zh'] = nl2br(strip_tags($strTrim->filter($this->_arrParam['footer-zh'])));
                $footer['en'] = nl2br(strip_tags($strTrim->filter($this->_arrParam['footer-en'])));
                $footer['trv'] = nl2br(strip_tags($strTrim->filter($this->_arrParam['footer-trv'])));
                
                $dataWebinfo = array(
                    'email'=>$email,
                );
                $this->_model->updateWebinfo($dataWebinfo, $id);
                foreach($footer as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'footer'=>$value,
                            );
                            $this->_model->updateWebinfoLanguage($data, $id, $lang['id']);
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/webinfo/success');
            }
        }
        
        public function successAction()
		{
		    $this->_helper->layout->setLayout('success');
		}
    }
